package com.xinlan.imageeditlibrary.editimage.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xinlan.imageeditlibrary.R;
import com.xinlan.imageeditlibrary.editimage.model.TextColorListEnum;
import com.xinlan.imageeditlibrary.editimage.utils.DensityUtil;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/9 22:45
 */
public class TextColorSelectListAdapter extends RecyclerView.Adapter<TextColorSelectListAdapter.TextColorSelectListHolder> {

    private LayoutInflater mInflater;
    private Context context;
    private TextColorListEnum[] values;
    private View lastView = null;

    public TextColorSelectListAdapter(LayoutInflater mInflater,Context context) {
        this.mInflater = mInflater;
        this.context = context;
    }

    public void setList(TextColorListEnum[] mList) {
        this.values = mList;
    }

    @NonNull
    @Override
    public TextColorSelectListHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new TextColorSelectListHolder(mInflater.inflate(R.layout.item_textcolorselectlist, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TextColorSelectListHolder textColorSelectListHolder, int i) {
        final TextColorListEnum value = values[i];
        textColorSelectListHolder.mTvColor.setBackgroundResource(value.getResId());
        textColorSelectListHolder.mTvColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GradientDrawable background = (GradientDrawable) view.getBackground();
                background.setStroke(DensityUtil.dip2px(context, 2), Color.parseColor("#F54D53"));
                if (lastView != null) {
                    GradientDrawable lastBackground = (GradientDrawable) lastView.getBackground();
                    lastBackground.setStroke(DensityUtil.dip2px(context, 2), Color.parseColor("#FFFFFF"));
                }
                    lastView = view;
                if (mOnTextColorClickListener != null)
                    mOnTextColorClickListener.onTextColorClick(value);
            }
        });
    }

    @Override
    public int getItemCount() {
        return values.length;
    }

    public class TextColorSelectListHolder extends RecyclerView.ViewHolder {

        private TextView mTvColor;

        public TextColorSelectListHolder(@NonNull View itemView) {
            super(itemView);
            mTvColor = itemView.findViewById(R.id.tv_item_textclolor);
        }
    }

    private OnTextColorClickListener mOnTextColorClickListener;

    public void setOnTextColorClickListener(OnTextColorClickListener onTextColorClickListener) {
        this.mOnTextColorClickListener = onTextColorClickListener;
    }

    public interface OnTextColorClickListener {
        void onTextColorClick(TextColorListEnum textColorListBean);
    }
}
